<template>
	<view class="login-container">
		<view class="login-header">
			<image class="logo" src="/static/logo.png"></image>
			<view class="title">欢迎登录</view>
		</view>
		
		<view class="login-form">
			<view class="input-item">
				<view class="icon">
					<text class="iconfont">👤</text>
				</view>
				<input 
					type="text" 
					v-model="username" 
					placeholder="请输入用户名"
					placeholder-style="color: #ccc;"
				/>
			</view>
			
			<view class="input-item">
				<view class="icon">
					<text class="iconfont">🔒</text>
				</view>
				<input 
					type="password" 
					v-model="password" 
					placeholder="请输入密码"
					placeholder-style="color: #ccc;"
				/>
			</view>
			
			<button class="login-btn" type="primary" @click="handleLogin">登录</button>
			
			<view class="other-options">
				<view class="forgot-password" @click="forgotPassword">忘记密码？</view>
				<view class="register" @click="register">注册账号</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import { ref } from 'vue';
	
	const username = ref('');
	const password = ref('');
	
	// 登录处理函数
	const handleLogin = () => {
		if (!username.value) {
			uni.showToast({
				title: '请输入用户名',
				icon: 'none'
			});
			return;
		}
		
		if (!password.value) {
			uni.showToast({
				title: '请输入密码',
				icon: 'none'
			});
			return;
		}
		uni.request({
			url: 'http://47.94.6.186:3006/api/users/login',
			method: 'POST',
			data: {
				username: username.value,
				password: password.value
			},
			success: (res) => {
				console.log(res);
				if (res.statusCode == 200) {
					uni.showToast({
						title: res.data.message
					});
					uni.setStorageSync('token', res.data.token);
					// uni.setStorageSync('userInfo', res.data.data.userInfo);
					uni.switchTab({
						url: '/pages/index/index'
					});
				} else {
					uni.showToast({
						title: res.data.message,
						icon: 'none'
					});
				}
			}
		});

		
		
		// // 模拟登录成功
		// uni.showLoading({
		// 	title: '登录中...'
		// });
		
		// setTimeout(() => {
		// 	uni.hideLoading();
		// 	uni.showToast({
		// 		title: '登录成功'
		// 	});
			
		// 	// 登录成功后返回上一页
		// 	setTimeout(() => {
		// 		uni.navigateBack();
		// 	}, 1500);
		// }, 1500);
	}
	
	// 忘记密码
	const forgotPassword = () => {
		uni.showToast({
			title: '忘记密码功能待实现',
			icon: 'none'
		});
	}
	
	// 注册账号
	const register = () => {
		uni.showToast({
			title: '注册功能待实现',
			icon: 'none'
		});
	}
</script>

<style scoped>
	.login-container {
		display: flex;
		flex-direction: column;
		min-height: 100vh;
		background-color: #f8f8f8;
	}
	
	.login-header {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 80rpx 0 60rpx 0;
	}
	
	.logo {
		width: 180rpx;
		height: 180rpx;
		margin-bottom: 30rpx;
	}
	
	.title {
		font-size: 40rpx;
		font-weight: bold;
		color: #333;
	}
	
	.login-form {
		padding: 0 40rpx;
	}
	
	.input-item {
		display: flex;
		align-items: center;
		background-color: #fff;
		border-radius: 8rpx;
		padding: 0 30rpx;
		margin-bottom: 30rpx;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
	}
	
	.icon {
		width: 60rpx;
		height: 60rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	
	.iconfont {
		font-size: 36rpx;
		color: #999;
	}
	
	input {
		flex: 1;
		height: 100rpx;
		font-size: 32rpx;
	}
	
	.login-btn {
		margin-top: 60rpx;
		height: 100rpx;
		line-height: 100rpx;
		font-size: 34rpx;
		border-radius: 8rpx;
	}
	
	.other-options {
		display: flex;
		justify-content: space-between;
		margin-top: 30rpx;
		padding: 0 20rpx;
	}
	
	.forgot-password,
	.register {
		font-size: 28rpx;
		color: #576b95;
	}
</style>
